var XmlDocument = require('xmldoc').XmlDocument;

//references..
function RefArea( id, order, boss_id, state, public, map_id, path_id )
{
    this.id = id;
    this.order = order;
    this.boss_id = boss_id;
    this.state = state;
    this.public = public;
    this.map_id = map_id;
    this.path_id = path_id;
};
var refAreas = new Array;

function GetRefArea( id )
{
    return refAreas[ id ];
}

function RefBoss( id, reward_exp, reward_gold, reward_refobj, refobj )
{
    this.id = id;
    this.reward_exp = reward_exp;
    this.reward_gold = reward_gold;
    this.reward_refobj = reward_refobj;
    this.refobj = refobj;
    
    this.deck = function( idx, refobj, level )
    {
        this.idx = idx;
        this.refobj = refobj;
        this.level = level;
    };
    this.deckInfos = new Array;
    
    this.SetDeck = function( idx, refobj, level )
    {
        var item = new this.deck( idx, refobj, level );
        this.deckInfos[ idx ] = item;
    }
};
var refBosses = new Array;

function RefCardRatio( rating, max_level )
{
    this.rating = rating;
    this.max_level = max_level;
};
var refCardRatios = new Array;

function Parse_RefCardRatios( fs )
{
    var data = fs.readFileSync('./reference/REF_CARDRATI.xml', 'utf8');
    
    var xml = new XmlDocument(data);
    var handler = function(child)
    {
        var reference = new RefCardRatio( child.attr.rating, child.max_level );
        refCardRatios[ reference.rating ] = reference;
    };
    xml.eachChild( handler );
}

function Parse_RefAreas( fs )
{
    var data = fs.readFileSync('./reference/REF_AREA.xml', 'utf8');
    
    var xml = new XmlDocument(data);
    var handler = function(child)
    {
        var attr = child.attr;
        if( attr.is_public != 1 )
            return;
        
        var reference = new RefArea( attr.id, attr.order, attr.boss_id, attr.state,
                                    attr.public, attr.map_id, attr.path_id );
        
        refAreas[ reference.id ] = reference;
    };
    xml.eachChild( handler );
}

function Parse_RefBosses( fs )
{
    var data = fs.readFileSync('./reference/REF_BOSS.xml', 'utf8');
    
    var xml = new XmlDocument(data);
    var handler = function(child)
    {
        var attr = child.attr;
        var reference = new RefBoss(attr.id,
                                    attr.reward_exp, attr.reward_gold, attr.reward_obj_ref_id, attr. obj_ref_id);
        
        reference.SetDeck( 1, attr.deck_obj_ref_id_1, attr.deck_level_1 );
        reference.SetDeck( 2, attr.deck_obj_ref_id_2, attr.deck_level_2 );
        reference.SetDeck( 3, attr.deck_obj_ref_id_3, attr.deck_level_3 );
        reference.SetDeck( 4, attr.deck_obj_ref_id_4, attr.deck_level_4 );
        reference.SetDeck( 5, attr.deck_obj_ref_id_5, attr.deck_level_5 );
        
        refBosses[ reference.id ] = reference;
    };
    xml.eachChild( handler );
}

exports.Parse_RefAreas = Parse_RefAreas;
exports.Parse_RefBosses = Parse_RefBosses;
exports.Parse_RefCardRatios = Parse_RefCardRatios;

exports.GetRefArea = GetRefArea;

